package com.zhuss.food.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.product.DownProductBo;
import com.zhuss.bean.vo.food.product.DetailProductVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.ProductDao;
import com.zhuss.food.db.entity.Product;
import com.zhuss.util.JsonUtil;

/**
 * 下架商品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("productDown")
public class ProductDown extends AbstractProcessor<Object, DownProductBo> implements Processor<Object, DownProductBo> {

    @Autowired
    private ProductDao productDao;
    
    @Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
    
    @Autowired
    private ProductCheck productCheck;
    
    /**
     * 校验
     */
    @Override
    public void check(DownProductBo bo) throws WpsException {
    	// 当前商品不满足下架条件
		if (!productCheck.checkDown(bo.getProductId())) {
			throw new WpsException(ExceptionCode.当前商品不满足下架条件);
		}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(DownProductBo param) throws WpsException {
    	
    	Long productId = param.getProductId();
    	
    	Product record = new Product();
    	record.setProductId(param.getProductId());
    	record.setStatus(param.getStatus());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	productDao.updateByPrimaryKeySelective(record);
    	
    	String name = ((DetailProductVo) productRead.detail(productId)).getName();
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DownProductBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.商品下架.key());
    	record.setTargetId(param.getProductId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
